﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using System.Data.Common;

namespace Cashier_Service
{
    public class MemberGradeService
    {
        /// <summary>
        /// 查询会员等级
        /// </summary>
        /// <param name="id"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public MemberGrade GetMemberGrade(long id, String code)
        {
            MemberGrade memberGrade = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    memberGrade = new MemberGrade();
                    memberGrade.Id = id;
                    memberGrade.Code = code;
                    memberGrade = new MemberGradeDAO().GetMemberGrade(conn, memberGrade);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return memberGrade;
        }

        /// <summary>
        /// 查询会员等级数据列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="ascfield"></param>
        /// <param name="descfield"></param>
        /// <returns></returns>
        public DataTable GetMemberGradeTable(int pageindex, int pagesize, String ascfield, String descfield)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    MemberGrade memberGrade = new MemberGrade();
                    memberGrade.Pageindex = pageindex;
                    memberGrade.Pagesize = pagesize;
                    memberGrade.Ascfield = ascfield;
                    memberGrade.Descfield = descfield;
                    dt = new MemberGradeDAO().GetMemberGradeTable(conn, memberGrade);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询会员等级记录数
        /// </summary>
        /// <returns></returns>
        public long GetMemberGradeRecord()
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    MemberGrade memberGrade = new MemberGrade();
                    record = new MemberGradeDAO().GetMemberGradeRecord(conn, memberGrade);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 插入会员等级
        /// </summary>
        /// <param name="memberGrade"></param>
        /// <returns></returns>
        public long InsertMemberGrade(MemberGrade memberGrade)
        {
            long id = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    id = new MemberGradeDAO().InsertMemberGrade(conn, memberGrade);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 更新会员等级信息
        /// </summary>
        /// <param name="memberGrade"></param>
        /// <returns></returns>
        public int UpdateMemberGrade(MemberGrade memberGrade)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    flag = new MemberGradeDAO().UpateMemberGrade(conn, memberGrade);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除会员等级
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteMemberGrade(String ids)
        {
            int count = 0;
            try
            {
                if (!String.IsNullOrEmpty(ids))
                {
                    using (DbConnection conn = ConnectionFactory.GetDbConnection())
                    {
                        conn.Open();
                        MemberGradeDAO memberGradeDAO = new MemberGradeDAO();
                        MemberGrade memberGrade = new MemberGrade();
                        String[] idArr = ids.Split(new Char[] { ',' });
                        foreach (String lopid in idArr)
                        {
                            memberGrade.Id = Int64.Parse(lopid);
                            count += memberGradeDAO.DeleteMemberGrade(conn, memberGrade);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }
    }
}
